import Vue from 'vue'
import VueRouter from 'vue-router'
//引入Login组件
import Login from '../components/Login.vue'
import Home from '../components/Home.vue'
import Welcome from '../components/Welcome.vue'
import UserList from '../components/admin/UserList.vue'

Vue.use(VueRouter)

const routes = [
  {
    path:"/",
    redirect:"/login"
  },
  {
    path:"/login",
    component: Login
  },
  {
    path:"/home",
    component: Home,
    redirect:"/welcome",
    children:[
      {path:"/welcome",component: Welcome},
      {path:"/user",component: UserList}

    ]
  }
]

const router = new VueRouter({
  routes
})
//挂载理由导航守卫
router.beforeEach((to,from,next)=>{
    //to将要访问
    //from从哪访问
    //next接着干next(url)重定向到url上
    if(to.path=='/login') return next();
    //获取user
    const userFlag = window.sessionStorage.getItem("user");
    if(!userFlag) return next('/login');//无值，返回登陆页
    next();
})

export default router
